package com.ashin.spring;

import com.ashin.spring.anno.Component;
import com.ashin.spring.anno.ComponentScan;
import com.ashin.spring.anno.Scope;

import java.io.File;
import java.net.URL;

/**
 * @Author: Ashin
 * @Date: 2021/9/25
 * @Description: com.ashin.spring.com.ashin
 * @Version: 1.0.0
 */
public class AnnotationConfigApplicationContext {


    public AnnotationConfigApplicationContext(Class<?> clazz) {

        boolean annotationPresent = clazz.isAnnotationPresent(ComponentScan.class);
        if (!annotationPresent) {
            return;
        }

        ComponentScan annotation = clazz.getAnnotation(ComponentScan.class);
        String packagePath = annotation.value();
        packagePath = packagePath.replaceAll("\\.", "/");

        ClassLoader classLoader = this.getClass().getClassLoader();
        URL resource = classLoader.getResource(packagePath);

        File file = new File(resource.getFile());
        if (file.isDirectory()) {
            for (File f : file.listFiles()) {
                System.out.println(f);
                String absolutePath = f.getAbsolutePath();

                String qualifiedClassName = absolutePath.substring(absolutePath.indexOf("com"), absolutePath.indexOf(".class"));
                try {
                    Class<?> aClass = classLoader.loadClass(qualifiedClassName.replaceAll(File.separator, "\\."));

                    if (aClass.isAnnotationPresent(Component.class)) {

                        if (aClass.isAnnotationPresent(Scope.class)) {
                            Scope scopeAnnotation = aClass.getAnnotation(Scope.class);
                            String value = scopeAnnotation.value();
                            if ("prototype".equals(value)) {
                                //单例
                            } else {
                                // 原型
                            }


                        } else {
                            //单例
                        }

                    }
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
            }
        }


    }


    public Object getBean(String beanName) {
        return null;
    }
}
